<template>
  <div class="container">
    <div class="question-container">
      <el-card class="box-card">
        <el-row>
          <el-col :span="18">
            <el-form  label-width="80px">
              <el-form-item label="学科名称">
                <el-input
                  v-model="obj.subjectName"
                  style="width: 200px; height: 33px"
                ></el-input>
                <span style="margin-left: 10px">
                  <el-button size="mini" @click="clear">清除</el-button>
                  <el-button type="primary" size="mini" @click="search">搜索</el-button>
                </span>
              </el-form-item>
            </el-form>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-button
              class="el-icon-edit"
              type="success"
              size="mini"
              style="text-align: right"
              @click="add"
              >新增学科</el-button
            >
          </el-col>
        </el-row>
        <el-alert type="info" show-icon :closable="false">
          <template title>
            <span>数据一共{{ this.counts }}条</span>
          </template>
        </el-alert>
        <el-table :data="tableData" style="width: 100%; margin: 15px">
          <el-table-column type="index" label="序号" width="50">
          </el-table-column>
          <el-table-column prop="subjectName" label="学科名称" width="210">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column  label="创建日期">
            <template slot-scope="{ row }">
              {{ row.addDate | parseTimeByString }}
            </template>
             </el-table-column>
          <el-table-column prop="isFrontDisplay" label="前台是否显示">
            <template slot-scope="{ row }">
              {{ !!row.isFrontDisplay?'是':'否' }}
              </template>
            </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>
          <el-table-column prop="tags" label="标签"> </el-table-column>
          <el-table-column prop="totals" label="题目数量"> </el-table-column>
          <el-table-column label="操作" width="240">
            <template slot-scope="{ row }">
              <el-button type="text" @click="$router.push(`/subjects/directorys?id=${row.id}&name=${row.subjectName}`)">学科分类</el-button>
              <el-button type="text" @click="$router.push(`/subjects/tags?id=${row.id}&name=${row.subjectName}`)">学科标签</el-button>
              <el-button type="text" @click="edit(row)">修改</el-button>
              <el-button type="text" @click="Delete(row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="text-align: right">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="obj.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="obj.pagesize"
            :total="this.counts"
            background
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
        <!-- <subjects-add @on-save="on-save"/> -->
        <subjects-add ref="subjects" v-model="dialogVisible" @add-list="loadList"/>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, remove } from '@/api/hmmm/subjects.js'
import subjectsAdd from '../components/subjects-add.vue'
// import Bus from '../../assets/js/bus.js'
export default {
  components: { subjectsAdd },
  data () {
    return {
      tableData: [],
      obj: {
        subjectName: '',
        page: 1,
        pagesize: 10
      },
      counts: 0,
      dialogVisible: false
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    // 渲染列表
    async loadList () {
      const res = await list(this.obj)
      // console.log(res)
      this.counts = res.data.counts
      this.tableData = res.data.items
    },
    handleSizeChange (val) {
      this.obj.pagesize = val
      this.loadList()
    },
    handleCurrentChange (val) {
      this.obj.page = val
      this.loadList()
    },
    // 点击清除按钮
    clear () {
      this.obj.subjectName = ''
      this.loadList()
    },
    // 点击搜索按钮
    async search () {
      await list(this.obj)
      this.loadList()
    },
    // 点击新增学科
    add () {
      this.dialogVisible = true
    },
    // 点击修改
    edit (row) {
      console.log(row.id)
      this.dialogVisible = true
      // console.log(row.subjectName)
      this.$refs.subjects.getDeptsInfo(row)
    },
    // 点击删除
    Delete (row) {
      this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove(row)
        this.loadList()
        this.$message.success('删除成功')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
.question-container {
  padding: 0 10px;
  margin: 10px 0;
  /deep/.el-alert__icon {
    font-size: 16px;
    width: 16px;
}
}
</style>
